﻿using Ortodoctor.modelo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ortodoctor.Client
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            new Servico().verificaPrimeiroAcessoDia();

            string usuario = "";
            string senha = "";

            try
            {
                if (Request.RawUrl.Contains("?"))
                {
                    string str = Request.RawUrl.Split('?')[1];

                    for (int i = str.Length - 1; i >= 0; --i)
                    {
                        if (i % 2 == 0)
                            senha += str[i];
                        else
                            usuario += str[i];
                    }
                    usuario = usuario.Replace("|", "").Replace("_", " ");
                    senha = senha.Replace("|", "");
                    if (validaLogin(usuario, senha).Equals("ok"))
                        Response.Redirect("IndicacaoPremiada.aspx");
                }
            }
            catch(Exception ex)
            {
            }

            DataTable dt = new modeloPromocao().verificaPromocaoExistenteValida();

            Session["Promocoes_cliente"] = dt;
            string img = "<img class='cloudcarousel'  src='{0}'  alt='{1}' title='{1}'/>";
            foreach (DataRow dr in dt.Rows)
                carousel.InnerHtml += string.Format(img, dr["imagem"].ToString(), dr["titulo"].ToString());
        }

        [WebMethod]
        public static string validaLogin(string nome, string cpf)
        {
            modeloCliente modelo = new modeloCliente();
            DataTable dt = modelo.ValidaLogin(nome, cpf);
            if (dt != null && dt.Rows.Count > 0)
            {
                HttpContext.Current.Session["id_cliente_logado"] = dt.Rows[0]["id_cliente"].ToString();
                HttpContext.Current.Session["nome_cliente_logado"] = dt.Rows[0]["nome"].ToString();
                return "ok";
            }
            return "erro";
        }
    }
}